package com.xnyzc.lhy.mis.service.impl.cms.area;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.ListToTreeUtil;
import com.xnyzc.lhy.mis.entity.area.OaSysArea;
import com.xnyzc.lhy.mis.mapper.area.OaSysAreaMapper;
import com.xnyzc.lhy.mis.service.cms.area.AreaTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  获取区域的树
 * </p>
 *
 * @author wangshuo
 * @since 2019-08-22
 */
@Service
public class AreaTreeServiceImpl implements AreaTreeService {

    @Autowired
    private OaSysAreaMapper oaSysAreaMapper;

    /**
     * 获取区域的树
     * @return
     */
    @Override
    public Rv getAreaJson() {
        Map<String, Object> map = new HashMap<>(16);
        QueryWrapper<OaSysArea> queryWrapper = new QueryWrapper<>();
        List<OaSysArea> areaList = oaSysAreaMapper.selectList(queryWrapper);
        List<Map<String, Object>> areaMapList =ListToTreeUtil.getAreaList(areaList);
        JSONArray resultList = ListToTreeUtil.listToTree(JSONArray.parseArray(JSON.toJSONString(areaMapList)),"id","pId","children");
        map.put("areaMapList",resultList);
        return Rv.wrap(EErrorCode.success, map);
    }

}
